package Chinese.Education.Identify.controller;

import Chinese.Education.Identify.pojo.Course;
import Chinese.Education.Identify.pojo.StudentCourse;
import Chinese.Education.Identify.service.StudentCourseService;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class StudentCourseController {

    @Autowired
    private StudentCourseService studentCourseService;

    @GetMapping("/courses/selectable")
    public Map<String, Object> getSelectableCourses(@RequestParam String major, @RequestParam String grade) {
        List<Course> selectableCourses = studentCourseService.getSelectableCourses(major, grade);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", selectableCourses.size());
        map.put("data", selectableCourses);
        return map;
    }

    @PostMapping("/student/{studentId}/courses")
    public void selectCourses(@PathVariable String studentId, @RequestBody Map<String, Object> payload) {
        List<String> courseIds = (List<String>) payload.get("courseIds");
        String semester = (String) payload.get("semester");
        studentCourseService.selectCourses(studentId, courseIds, semester);
    }

    @GetMapping("/student/{studentId}/courses")
    public Map<String, Object> getStudentCourses(@PathVariable String studentId) {
        List<StudentCourse> studentCourses = studentCourseService.getStudentCourses(studentId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", studentCourses.size());
        map.put("data", studentCourses);
        return map;
    }

    @GetMapping("/teacher/{teacherId}/students")
    public Map<String, Object> getStudentsForTeacher(@PathVariable String teacherId) {

        List<StudentCourse> studentCourses = studentCourseService.getStudentsForTeacher(teacherId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", studentCourses.size());
        map.put("data", studentCourses);
        return map;
    }

    @PutMapping("/student-course/{id}/grade")
    public void gradeStudent(@PathVariable Integer id, @RequestBody Map<String, BigDecimal> scores) {
        studentCourseService.gradeStudent(id, scores.get("regularScore"), scores.get("labScore"), scores.get("examScore"));
    }

    @GetMapping("/student-courses")
    public Map<String, Object> searchStudentCourses(@RequestParam(required = false) String studentId,
                                                    @RequestParam(required = false) String courseId,
                                                    @RequestParam(required = false) String semester) {
        List<StudentCourse> list = studentCourseService.searchStudentCourses(studentId, courseId, semester);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }
}
